package com.kaili.finance.controller.web.v1;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.common.aop.OperateLogAspect;
import com.kaili.common.aop.log.enums.FunctionName;
import com.kaili.common.aop.log.enums.OperateType;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.util.LoginDetailUtils;
import com.kaili.common.util.SnowflakeIdFactory;
import com.kaili.common.util.ValidList;
import com.kaili.finance.log.FinRechargeWithdrawLogDict;
import com.kaili.finance.pojo.dto.account.*;
import com.kaili.finance.pojo.entity.FinAccountDetail;
import com.kaili.finance.pojo.entity.FinRecharge;
import com.kaili.finance.pojo.entity.FinTransferAcccounts;
import com.kaili.finance.pojo.vo.*;
import com.kaili.finance.service.AccountService;
import com.kaili.finance.version.ApiVersion;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by lsw on 18-5-17.
 */
@ApiVersion(1)
@RequestMapping("/{api_version}")
@RestController
@Api(tags = "预付款账户", description = "账户开户充值转账API")
public class FinAccountController {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private AccountService accountService;

    @GetMapping("/finAccount")
    @ApiOperation(value = "根据用户ID或则用户名,带出账户id和用户名", notes = "根据账号或账号ID查询账户信息API")
    public List<FinAccountInfoVO> queryAccountByLike(@ModelAttribute FinAccountInfoQueryDTO dto){
        List<FinAccountInfoVO> list=accountService.queryAccountByLike(dto.getAccountIdOrName());
        return list;
    }

    @GetMapping("/finAccountName")
    @ApiOperation(value = "模糊查询未开户的网点、员工、客户", notes = "模糊查询未开户的网点、员工、客户API")
    public List<FinNewAccountInfoVO> queryAccountNameByLike(@ModelAttribute FinAccountInfoQueryDTO dto){
        List<FinNewAccountInfoVO> list=accountService.queryAccountNameByLike(dto.getAccountIdOrName());
        return list;

    }

    @GetMapping("/loginAccount")
    @ApiOperation(value = "查询当前登陆用户的充值/提现信息", notes = "查询当前登陆用户的充值/提现信息API")
    public FinLoginAccountVO queryAccountByLogin(){
        FinLoginAccountVO vo = accountService.queryAccountByLogin(LoginDetailUtils.getCurrentEmployeeId());
        return vo;
    }

    @GetMapping("/newAccountId")
    @ApiOperation(value = "自动获取开户id", notes = "自动获取开户id")
    public Long autoCreateAccountId(){
        return accountService.autoCreateAccountId();
    }

    @GetMapping("/accounts")
    @ApiOperation(value = "根据用户ID查找对应账户信息,用户id为空则查询所有账户", notes = "根据用户ID查找对应账户信息,用户id为空则查询所有账户API")
    public Page<FinAccountDetailVO> queryAccountById(@ModelAttribute FinAccountDetailQueryDTO dto){
        Page<FinAccountDetailVO> data = accountService.queryAccountById(new Page<FinAccountDetailVO>(dto.getPage(), dto.getPageSize()), dto.getAccountId());
        return data;
    }

    @PostMapping("/accounts")
    @ApiOperation(value = "账户开户", notes = "新增开户信息API")
    @OperateLogAspect(functionName = FunctionName.openAccount,operateType = OperateType.ADD, key = "accountId", dict = FinRechargeWithdrawLogDict.class)
    public CommonStatus addAccount(@Validated @RequestBody FinAccountDetailAddDTO dto){
        FinAccountDetail finAccountVO = new FinAccountDetail();
        BeanUtils.copyProperties(dto, finAccountVO);
        try {
            Integer createdId = LoginDetailUtils.getCurrentEmployeeId();
            //获取当前登陆用户的id
            if(createdId==null){
                finAccountVO.setCreatedBy(-1);
            }else {
                finAccountVO.setCreatedBy(createdId);
            }
            //设置创建时间 状态
            finAccountVO.setCreatedTime(new Date());
            //设置账户状态
            finAccountVO.setStatus(1);
            //设置更改日期
            finAccountVO.setModifyTime(new Date());
            //修改人
            if(createdId==null){
                finAccountVO.setModifyBy(-1);
            }else {
                finAccountVO.setModifyBy(createdId);
            }
            CommonStatus status=accountService.addAccount(finAccountVO);
            return status;
        }catch (Exception e){
            e.printStackTrace();
            return CommonStatus.ACC_ADD_FAIL;
        }
    }

    @PutMapping("/accounts")
    @ApiOperation(value = "修改或冻结账户", notes = "修改或删除账户API")
    public CommonStatus updateAccount(@Validated @RequestBody ValidList<FinAccountUpdateDTO> accountList){
        return accountService.updateAccount(accountList);
    }

    @PostMapping("/transferAccounts")
    @ApiOperation(value = "账户之间转账", notes = "账户之间转账API")
    public RestStatus addTranfer(@Validated @RequestBody FinTransferAccountsAddDTO dto){
        FinTransferAcccounts fInAdjustVO = new FinTransferAcccounts();
        BeanUtils.copyProperties(dto, fInAdjustVO);
        try{
            //数据来源
            fInAdjustVO.setSourceType('1');
            //费用项目
            fInAdjustVO.setChargeItemId(8);
            //获取创建人
            Integer createdId = LoginDetailUtils.getCurrentEmployeeId();
            //获取当前登陆用户的id
            if(createdId==null){
                fInAdjustVO.setCreatedBy(-1);
            }else {
                fInAdjustVO.setCreatedBy(createdId);
            }

            fInAdjustVO.setCreatedTime(new Date());
            //生成运单号
            fInAdjustVO.setEwbNo(SnowflakeIdFactory.getNextId());
            CommonStatus status = accountService.addTranfer(fInAdjustVO);
            return status;
        }catch (Exception e){
             e.printStackTrace();
            return  CommonStatus.ACC_FAIL;
        }
    }

    @GetMapping("/transferAccounts")
    @ApiOperation(value = "查询转账信息", notes = "查询转账信息API")
    public Page<FinTransferAccountsDetailVO> queryAdjustRecord(@ModelAttribute FinTransferAccountsQueryDTO dto){
        logger.info("Enter in queryAdjustRecord.This is info.");
        logger.debug("Enter in queryAdjustRecord.This is debug.");
        logger.warn("Enter in queryAdjustRecord.This is warn.");
        logger.error("Enter in queryAdjustRecord.This is error.");

        Page<FinTransferAccountsDetailVO> data=accountService.queryAdjustRecord(new Page<FinTransferAccountsDetailVO>(dto.getPage(),dto.getPageSize()),dto.getInAccountId(),dto.getOutAccountId());
        return data;
    }


    @PostMapping("/reCharge")
    @ApiOperation(value = "新增充值/提现", notes = "新增充值/提现API")
    public RestStatus addRechargeWithdraw(@Validated @RequestBody FinRechargeAddDTO dto){
        FinRecharge finRechargeWithdrawVO = new FinRecharge();
        BeanUtils.copyProperties(dto, finRechargeWithdrawVO);
        try{
            Integer integer = LoginDetailUtils.getCurrentEmployeeId();
            finRechargeWithdrawVO.setEwbNo(SnowflakeIdFactory.getNextId());
            finRechargeWithdrawVO.setSourceType('2');

            Integer createdId = LoginDetailUtils.getCurrentEmployeeId();
            //获取当前登陆用户的id
            if(createdId==null){
                finRechargeWithdrawVO.setCreatedBy(-1);
            }else {
                finRechargeWithdrawVO.setCreatedBy(createdId);
            }

            finRechargeWithdrawVO.setCreatedTime(new Date());
            CommonStatus status = accountService.addRechargeWithdraw(finRechargeWithdrawVO);
            return status;
        }catch (Exception e){
            e.printStackTrace();
            return CommonStatus.ACC_FAIL;
        }
    }

    @GetMapping("/reCharge")
    @ApiOperation(value = "查询充值/提现记录", notes = "查询充值/提现记录API")
    public Page<FinRechargeVO> queryRechargeWithdrawRecord(@ModelAttribute FinRechargeQueryDTO dto){
        Page<FinRechargeVO> data= accountService.queryRechargeWithdrawRecord(dto.getChargeType(), dto.getAccountId(),
                                          new Page<FinRechargeVO>(dto.getPage(),dto.getPageSize()));
        return data;
    }


}
